package com.hospital.workstation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Console;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.HisMedicalOrders;
import com.hospital.workstation.domain.bo.PatientBaseInfoBo;
import com.hospital.workstation.domain.bo.TcmExportBo;
import com.hospital.workstation.domain.bo.TcmPrescriptionBo;
import com.hospital.workstation.domain.vo.HisMedicalOrdersVo;
import com.hospital.workstation.domain.vo.PatientBaseInfoVo;
import com.hospital.workstation.domain.vo.TcmPrescriptionVo;
import com.hospital.workstation.service.IPatientBaseInfoService;
import com.hospital.workstation.service.ITcmPrescriptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 中医证型方剂信息
 *
 * @author whb
 * @date 2023-06-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/tcmPrescription")
public class TcmPrescriptionController extends BaseController {

    private final ITcmPrescriptionService iTcmPrescriptionService;
    private final IPatientBaseInfoService baseInfoService;

    /**
     * 获取导入模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "中医证型方剂信息导入模板", TcmPrescriptionVo.class, response);
    }

    /**
     * 导入中医证型方剂数据
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<TcmPrescriptionVo> tcmPrescriptionVoList = ExcelUtil.importExcel(file.getInputStream(), TcmPrescriptionVo.class);
        return iTcmPrescriptionService.addBatchTcmPrescription(tcmPrescriptionVoList);
    }

    /**
     * 查询下拉列表数据
     *
     * @param bo    证型方剂业务对象
     * @param level 病名和证型下拉分级标识（1 病名 2证型）
     * @return
     */
    @GetMapping("/level/list")
    public R<List<String>> getTcmSelectList(TcmPrescriptionBo bo, String level) {
        return iTcmPrescriptionService.getTcmSelectList(bo, level);
    }

    /**
     * 查询患者信息列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @SaCheckPermission("workstation:tcmPrescription:list")
    @GetMapping("/list")
    public TableDataInfo<PatientBaseInfoVo> getPatientBaseInfoList(PatientBaseInfoBo bo, PageQuery pageQuery) {
        return baseInfoService.getPatientBaseInfoListOfTcm(bo, pageQuery);
    }

    /**
     * 查询病症集合
     *
     * @param medicalRecordNo 流水号
     * @param diagType        诊断类型（2出院诊断 3入院诊断）
     * @return
     */
    @GetMapping("/name/{medicalRecordNo}/{diagType}")
    public R<Map<String, List<String>>> getIllnessAndSymptom(@PathVariable Integer medicalRecordNo, @PathVariable String diagType) {
        return baseInfoService.getIllnessAndSymptomByMedicalRecordNo(medicalRecordNo, diagType);
    }

    /**
     * 查询中药饮片
     *
     * @param medicalRecordNo 流水号
     * @return
     */
    @GetMapping("/traditional/{medicalRecordNo}")
    public R<Map<String, List<HisMedicalOrdersVo>>> getTraditionalMedicalOrdersList(@PathVariable Integer medicalRecordNo) {
        return iTcmPrescriptionService.getTraditionalMedicalOrdersByMedicalRecordNo(medicalRecordNo);
    }

    /**
     * 审核
     *
     * @param bo
     * @return
     */
    @PostMapping("/audit")
    public R<List<TcmPrescriptionVo>> audit(@Validated(AddGroup.class) @RequestBody TcmPrescriptionBo bo) {
        return iTcmPrescriptionService.audit(bo);
    }

    /**
     * 导出审核结果
     *
     * @param response
     * @param exportBo 中药饮片处方导出业务对象
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody TcmExportBo exportBo) {
        iTcmPrescriptionService.export(response, exportBo);
    }
}
